var express=require("express")();
var mysql=require("mysql");
var url=require("url");
var expressStatic=require("express-static");
var port=8081;

express.all("/*", function(req, res, next) {
    // 跨域处理
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By", ' 3.2.1');
    res.header("Content-Type", "application/json;charset=utf-8");
    next(); // 执行下一个路由
})

var sql = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : '123456',
  database : 'canyin'
});
sql.connect();

//获取所有餐桌信息接口
express.get("/tableList",(request,response)=>{
	sql.query(`SELECT * FROM tablelist`,(error,result)=>{
		if(error){
			console.log(error)
			response.end("error")
		}
		else{
			response.end(JSON.stringify(result))
		}
	})
})
//获取菜品信息接口
express.get("/foodList",(request,response)=>{
	sql.query(`SELECT * FROM foodlist`,(error,result)=>{
		if(error){
			console.log(error)
			response.end("error")
		}
		else{
			response.end(JSON.stringify(result))
		}
	})
})
//提交账单信息接口
express.get("/submitBill",(request,response)=>{
	var tableid=url.parse(request.url,true).query.tableid
	var foodslist=url.parse(request.url,true).query.foodslist
	var total=url.parse(request.url,true).query.total
	var submitdate=url.parse(request.url,true).query.submitdate
	//查看该桌是否有未结账的订单
	sql.query(`SELECT * FROM billlist WHERE tableid=${tableid} AND state=1`,(error,result)=>{
		if(error){
			console.log(error)
			response.end("error")
		}
		else{
			if(result.length>0){
				//该桌存在未结账订单，也就是有顾客在用餐，修改菜单（加菜）
				sql.query(`UPDATE billlist SET foodslist='${foodslist}',total=${total} WHERE tableid=${tableid} AND state=1`,(error,result)=>{
					if(error){
						console.log(error)
						response.end("error")
					}
					else{
						console.log(`${tableid}桌新增菜品`)
						response.end("success")
					}
				})
			}
			else{
				//该桌是空桌，新生成订单
				sql.query(`INSERT INTO billlist (tableid,foodslist,total,time,state) VALUES (${tableid},'${foodslist}',${total},"${submitdate}",1)`,(error,result)=>{
					if(error){
						console.log(error)
						response.end("生成订单失败")
					}
					else{
						console.log(`${tableid}号桌下单成功！`)
						response.end("success")
					}
				})
				//修改餐桌为用餐状态
				sql.query(`UPDATE tablelist SET state = 1 WHERE zhuid = ${tableid}`,(error,result)=>{
					if(error){
						console.log(error)
						response.end("修改餐桌状态失败")
					}
					else{
						console.log(`${tableid}号桌改为用餐状态！`)
					}
				})
			}
		}
	})
})
//获取当前桌已点但未结的账单信息
express.get("/historyFoodList",(request,response)=>{
	var tableid=url.parse(request.url,true).query.tableid
	sql.query(`SELECT * FROM billlist WHERE tableid=${tableid} AND state=1`,(error,result)=>{
		if(error){
			console.log(error)
			response.end("error")
		}
		else{
			if(result.length>0){
				console.log(`${tableid}号桌历史菜单获取成功`)
				response.end(JSON.stringify(result))
			}
		}
	})
})
//获取所有未结账账单信息
express.get("/billList",(request,response)=>{
	sql.query(`SELECT * FROM billlist WHERE state=1`,(error,result)=>{
		if(error){
			console.log(error)
			response.end("error")
		}
		else{
			if(result.length>0){
				response.end(JSON.stringify(result))
			}
		}
	})
})
//不结账强行结束账单设置空闲桌
express.get("/setFree",(request,response)=>{
	var tableid=url.parse(request.url,true).query.tableid
	sql.query(`UPDATE billlist SET state=0 WHERE tableid=${tableid} AND state=1`,(error,result)=>{
		if(error){
			console.log(error)
			response.end("error")
		}
		else{
			sql.query(`UPDATE tablelist SET state=0 WHERE zhuid=${tableid}`,(error,result)=>{
				if(error){
					console.log(error)
					response.end("error")
				}
				else{
					console.log(`已关闭${tableid}号桌当前账单并设为空闲桌`)
					response.end("success")
				}
			})
		}
	})
})
//添加新餐桌
express.get("/addTable",(request,response)=>{
	var tableType=url.parse(request.url,true).query.tableType
	sql.query(`INSERT INTO tablelist (state, seat) VALUES (0, ${tableType})`,(error,result)=>{
		if(error){
			console.log(error)
			response.end("error")
		}
		else{
			console.log(`${tableType}人桌添加成功`)
			response.end("success")
		}
	})
})
//修改菜品信息
express.get("/reviseFood",(request,response)=>{
	var foodid=url.parse(request.url,true).query.foodid
	var foodname=url.parse(request.url,true).query.foodname
	var price=url.parse(request.url,true).query.price
	var type=url.parse(request.url,true).query.type
	var sign=url.parse(request.url,true).query.sign
	sql.query(`UPDATE foodlist SET price=${price},type="${type}",sign=${sign} WHERE foodid=${foodid}`,(error,result)=>{
		if(error){
			console.log(error)
			response.end("error")
		}
		else{
			console.log(`修改${foodname}菜品信息成功`)
			response.end("success")
		}
	})
})
//删除菜品信息
express.get("/deleteFood",(request,response)=>{
	var foodid=url.parse(request.url,true).query.foodid
	var foodname=url.parse(request.url,true).query.foodname
	sql.query(`DELETE FROM foodlist WHERE foodid=${foodid}`,(error,result)=>{
		if(error){
			console.log(error)
			response.end("error")
		}
		else{
			console.log(`删除${foodname}菜品信息成功`)
			response.end("success")
		}
	})
})
//添加菜品信息
express.get("/addBill",(request,response)=>{
	var foodname=url.parse(request.url,true).query.foodname
	var price=url.parse(request.url,true).query.price
	var type=url.parse(request.url,true).query.type
	var sign=url.parse(request.url,true).query.sign
	sql.query(`INSERT INTO foodlist (foodname, price, type, sign) VALUES ("${foodname}",${price},"${type}",${sign})`,(error,result)=>{
		if(error){
			console.log(error)
			response.end("error")
		}
		else{
			console.log(`${foodname}添加成功`)
			response.end("success")
		}
	})
})

setInterval(()=>{
	sql.query(`SELECT * FROM tablelist`,(error,result)=>{
		if(error){
			console.log(error)
		}
	})
},25200000)
express.listen(port);
console.log(`Sever is running at ${port}`)